﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UCNKompetenceBors.Database;

namespace UCNKompetenceBors.Admin
{
    public partial class adminDepartmentCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindSubMenu();
                lblCreatedepartmentSucces.Visible = false;

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);

                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkAdminDashboard.NavigateUrl = "~/Admin/admin.aspx";
            hyLinkUserHandling.NavigateUrl = "~/Admin/adminUsers.aspx";
            hyLinkAdminExchange.NavigateUrl = "~/Admin/adminExchange.aspx";
            hyLinkNewsHandling.NavigateUrl = "~/Admin/adminNews.aspx";
            hyLinkEditInformation.NavigateUrl = "~/Admin/adminInformation.aspx";
            hyLinkResetUsers.NavigateUrl = "~/Admin/adminUsersReset.aspx";
            hylinkContactPersons.NavigateUrl = "~/Admin/adminContactPerson.aspx";
            hylinkDepartment.NavigateUrl = "~/Admin/adminDepartment.aspx";
            hylinkDepartmentCreate.NavigateUrl = "~/Admin/adminDepartmentCreate.aspx";
        }

        /// <summary>
        /// Creates a department, based on the input parameters.
        /// </summary>
        /// <param name="name">Specifies the department name to be used.</param>
        /// <param name="hasImage">Specifies whether or not contact person has an image.</param>
        /// <param name="imageFileName">Specifies the file name of the image to be used.</param>
        /// <param name="address">Specifies the address to be used.</param>
        /// <param name="zipCode">Specifies the zip code to be used.</param>
        /// <param name="city">Specifies the city to be used.</param>
        /// <param name="email">Specifies the email to be used in the contact person.</param>
        /// <param name="phone">Specifies the phone to be used in the contact person.</param>
        /// <param name="fax">Specifies the fax to be used.</param>
        public static void CreateDepartment(string name, bool hasImage, string imageFileName, string address, string zipCode, string city, string email, string phone, string fax)
        {
            dbAdmin.CreateDepartment(name, hasImage, imageFileName, address, zipCode, city, email, phone, fax);
        }

        /// <summary>
        /// Uploads an image.
        /// </summary>
        private void UploadPicture()
        {
            string path = Server.MapPath("~/Images/Departments/" + txtCreateDepartmentName.Text.Replace(" ", "") + "/");
            if (!Directory.Exists(path))
            {
                DirectoryInfo dir = new DirectoryInfo(path);

                dir.Create();
            }

            if (fuCreateDepartmentImage.HasFile)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(fuCreateDepartmentImage.FileContent);
                string imagePath = path + fuCreateDepartmentImage.FileName;

                GeneralTools.CreateThumbnail(100, image, imagePath);
            }
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            hyLinkAdminDashboard.Text = GetLocalResourceObject("hyLinkAdminDashboardText").ToString();
            hyLinkUserHandling.Text = GetLocalResourceObject("hyLinkUserHandlingText").ToString();
            hyLinkAdminExchange.Text = GetLocalResourceObject("hyLinkAdminExchangeText").ToString();
            hyLinkNewsHandling.Text = GetLocalResourceObject("hyLinkNewsHandlingText").ToString();
            hyLinkEditInformation.Text = GetLocalResourceObject("hyLinkEditInformationText").ToString();
            hyLinkResetUsers.Text = GetLocalResourceObject("hyLinkResetUsersText").ToString();
            hylinkDepartmentCreate.Text = GetLocalResourceObject("hylinkDepartmentCreateText").ToString();
            hylinkContactPersons.Text = GetLocalResourceObject("hylinkContactPersonsText").ToString();
            hylinkDepartment.Text = GetLocalResourceObject("hylinkDepartmentText").ToString();

            lblAdminContentHeader.Text = GetLocalResourceObject("lblAdminContentHeaderText").ToString();
            lblCreatedepartmentSucces.Text = GetLocalResourceObject("lblCreatedepartmentSuccesText").ToString();
            lblCreateDepartmentName.Text = GetLocalResourceObject("lblCreateDepartmentNameText").ToString();
            lblCreateDepartmentAddress.Text = GetLocalResourceObject("lblCreateDepartmentAddressText").ToString();
            lblCreateDepartmentZipCity.Text = GetLocalResourceObject("lblCreateDepartmentZipCityText").ToString();
            lblCreateDepartmentEmail.Text = GetLocalResourceObject("lblCreateDepartmentEmailText").ToString();
            lblCreateDepartmentPhone.Text = GetLocalResourceObject("lblCreateDepartmentPhoneText").ToString();
            lblCreateDepartmentFax.Text = GetLocalResourceObject("lblCreateDepartmentFaxText").ToString();
            lblCreateDepartmentImage.Text = GetLocalResourceObject("lblCreateDepartmentImageText").ToString();

            btnCreateDepartment.Text = GetLocalResourceObject("btnCreateDepartmentText").ToString();

            string imageRegexValidaton = GetLocalResourceObject("regexvalImageText").ToString();
            regexvalImage.ToolTip = imageRegexValidaton;
            regexvalImage.ErrorMessage = imageRegexValidaton;

            string phoneValidation = GetLocalResourceObject("rfvalPhoneText").ToString();
            rfvalPhone.ToolTip = phoneValidation;
            rfvalPhone.ErrorMessage = phoneValidation;

            string phoneRegexValidation = GetLocalResourceObject("regexvalPhoneText").ToString();
            regexvalPhone.ToolTip = phoneRegexValidation;
            regexvalPhone.ErrorMessage = phoneRegexValidation;

            string faxRegexValidation = GetLocalResourceObject("regexvalFaxText").ToString();
            regexvalFax.ToolTip = faxRegexValidation;
            regexvalFax.ErrorMessage = faxRegexValidation;

            string EmailRegexValidation = GetLocalResourceObject("regexvalEmailText").ToString();
            regexvalEmail.ToolTip = EmailRegexValidation;
            regexvalEmail.ErrorMessage = EmailRegexValidation;

            string nameValidation = GetLocalResourceObject("rfvalNameText").ToString();
            rfvalName.ToolTip = nameValidation;
            rfvalName.ErrorMessage = nameValidation;

            string AddressValidation = GetLocalResourceObject("rfvalAddressText").ToString();
            rfvalAddress.ToolTip = AddressValidation;
            rfvalAddress.ErrorMessage = AddressValidation;

            string emailValidation = GetLocalResourceObject("rfvalEmailText").ToString();
            rfvalEmail.ToolTip = emailValidation;
            rfvalEmail.ErrorMessage = emailValidation;

            string ZipValidation = GetLocalResourceObject("rfvalZipCodeText").ToString();
            rfvalZipCode.ToolTip = ZipValidation;
            rfvalZipCode.ErrorMessage = ZipValidation;

            string CityValidation = GetLocalResourceObject("rfvalCityText").ToString();
            rfvalCity.ToolTip = CityValidation;
            rfvalCity.ErrorMessage = CityValidation;

            valSumCreateDepartment.HeaderText = GetLocalResourceObject("valSumCreateDepartmentText").ToString();
        }

        protected void btnCreateDepartment_Click(object sender, EventArgs e)
        {
            string name = txtCreateDepartmentName.Text;
            string address = txtCreateDepartmentAddress.Text;
            bool hasImage = fuCreateDepartmentImage.HasFile;
            string imageFileName = fuCreateDepartmentImage.FileName;
            string phone = txtCreateDepartmentPhone.Text;
            string email = txtCreateDepartmentEmail.Text;
            string fax = txtCreateDepartmentFax.Text;
            string zipCode = txtCreateDepartmentZipCode.Text;
            string city = txtCreateDepartmentCity.Text;

            //if (HttpContext.Current.Session["MyCulture"] != null)
            //    culture = HttpContext.Current.Session["MyCulture"].ToString();

            UploadPicture();
            CreateDepartment(name, hasImage, imageFileName, address, zipCode, city, email, phone, fax);
            panAdminCreateDepartment.Visible = false;
            lblCreatedepartmentSucces.Visible = true;
        }
    }
}